<?php


namespace App\HttpController;

use EasySwoole\Queue\Job;
use EasySwoole\Http\AbstractInterface\Controller;
use App\Models\AdminModel;
use App\Models\UserModel;
use EasySwoole\Component\Di;
use App\HttpController\UserBase;
use App\Models\Project;

use App\Models\History;
use App\service\MyQueue;
class Sly  extends UserBase
{
    
        
        public function onRequest(?string $action): ?bool
    {
        if (!parent::onRequest($action)) {
            return false;
        }

       if($this->who['type']!=1){
        
               $this->writeJson(403,"u r not permitted!");
        
       
       }
        return true;
    }

    function detail(){
      $data = $this->request()->getRequestParam();
      $id = intval($data['id']);
      $item  =Project::create()->get(['id'=>$id]);
  
      $this->writeJson(200,$item);
    }


  function approvelist(){
    $limit = \EasySwoole\EasySwoole\Config::getInstance()->getConf('pagelimit');
    $model = Project::create();
    $page = $this->input('page',1);
    $owner = $this->input("owner","");
    $lxnumber = $this->input("lxnumber","");
    $name = $this->input("name","");
    $wtdw = $this->input("wtdw","");
    $bzjg = $this->input("bzjg","");
    $addtime = $this->input("addtime","");
    $industry = $this->input("industry","");
    $wtdwcontactor = $this->input("wtdwlxr","");
    $pslb = $this->input("pslb","");
    $where = [];
    if($owner == 'null'){
        $owner = "";
    }

    if($industry == 'null' || $industry == "1"){
        $industry = "";
    }

    if($lxnumber == 'null'){
        $lxnumber = "";
    }
    if($name == 'null'){
        $name = "";
    }
    if($wtdw == 'null'){
        $wtdw = "";
    }
    if($bzjg == 'null'){
        $bzjg = "";
    }
    if($pslb == 'null'){
        $pslb = "";
    }
    if($wtdwcontactor == 'null'){
        $wtdwcontactor = "";
    }
   

    if($owner){
        $model = $model->where("owner","%".$owner."%",'like');
    }

    if($lxnumber){
   
        $model = $model->where("lxnumber","%".$lxnumber."%",'like');
    }
    if($name){
        $model = $model->where("name","%".$name."%",'like');
    }
    if($wtdw){
        $model = $model->where("wtdw","%".$wtdw."%",'like');
    }
    if($bzjg){
        $model = $model->where("bzjg","%".$bzjg."%",'like');
    }
    
    if($industry){
        $model = $model->where(["industry"=>$industry]);
    }

    if($pslb && $pslb!='1'){
        $model = $model->where(['approve_type'=>$pslb]);
    }
    if($wtdwcontactor){
        $model = $model->where("wtdwcontactor","%".$wtdwcontactor."%",'like');
    }
      $st = $this->input("st");
      if($st=="done"){
        $list = $model->where('status',[1,-1],'in');
      }else{
        $list = $model->where(['status'=>0]);
      }

      $model = $model->order('id', 'DESC')->limit($limit * ($page - 1), $limit)->withTotalCount();
      $list = $model->all(null);
      $result = $model->lastQueryResult();
      $total = $result->getTotalCount();
      $pagecon = $this->showPageStringAsAJAXForitem("jumpage",$page,$limit,$total,$owner);
      $this->writeJson(200,['list'=>$list,'pg'=>$pagecon]);
  }

  
  function passitem(){
    $data = $this->request()->getRequestParam();
    $id = $data['id'];
    $item  =Project::create()->get(['id'=>intval($id)]);
    if ($item['status'] != 0) {
        $this->writeJson(500,'','this project has a error status');
        return false;
    }
    $res = Project::create()->update(['status'=>1],['id'=>$id,'status'=>0]);
    if($res){
      $job = new job();
      $job->setJobData(intval($id));
      $queue = MyQueue::getInstance();
      $queue->producer()->push($job);
      }
    $this->writeJson(200,'','ok to update');
  }



  function rejectitem(){

    $data = $this->request()->getRequestParam();
    $id = $data['id'];
    $item  =Project::create()->get(['id'=>intval($id)]);
   
    if ($item['status'] != 0) {
        $this->writeJson(500,'','this project has a error status');
        return false;
    }
    $data = ['userid'=>$this->who['id'],'approveres'=>0,
    'idea'=>$data['reason'],'isgroup'=>0,'usertype'=>$this->who['type'],'itemid'=>$id,
  "addtime"=>time()];

    var_dump($data);


    History::create($data)->save();

    $res = Project::create()->update(['status'=>-1],['id'=>intval($id)]);
    var_dump($res);
    $this->writeJson(200,'','ok');

  }


   
} 
